/*
Copyright 2017 yangchong211（github.com/yangchong211）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.yc.video.ui.view;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import com.yc.video.ResourceTable;
import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.tool.BaseToast;
import com.yc.video.tool.PlayerUtils;

/**
 * <pre>
 *     @author yangchong
 *     blog  : https://github.com/yangchong211
 *     time  : 2017/11/9
 *     desc  : 即将开播视图
 *     revise:
 * </pre>
 */
public class CustomOncePlayView extends DirectionalLayout implements InterControlView, Component.TouchEventListener {
    private Context mContext;
    private Text mTvMessage;
    private Text mTvRetry;
    private int playState;
    private ControlWrapper mControlWrapper;

    public CustomOncePlayView(Context context) {
        super(context);
        init(context);
    }

    public CustomOncePlayView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomOncePlayView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        setVisibility(HIDE);
        Component component = LayoutScatter.getInstance(getContext()).parse(
            ResourceTable.Layout_custom_video_player_once_live, this, true);
        initFindViewById(component);
        initListener();
        setClickable(true);
    }

    private void initFindViewById(Component component) {
        mTvMessage = (Text) component.findComponentById(ResourceTable.Id_tv_message);
        mTvRetry = (Text) component.findComponentById(ResourceTable.Id_tv_retry);
    }

    private void initListener() {
        mTvRetry.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (playState == ConstantKeys.CurrentState.STATE_ONCE_LIVE) {
                    // 即将开播
                    if (PlayerUtils.isConnected(mContext)) {
                        mControlWrapper.start();
                    } else {
                        BaseToast.showRoundRectToast("请查看网络是否连接");
                    }
                } else {
                    BaseToast.showRoundRectToast("时间还未到，请稍后再试");
                }
            }
        });
    }

    @Override
    public void attach(ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animator anim) {
    }

    @Override
    public void onPlayStateChanged(int state) {
        this.playState = state;
        if (state == ConstantKeys.CurrentState.STATE_ONCE_LIVE) {
            // 即将开播
            setVisibility(VISIBLE);
        } else {
            setVisibility(HIDE);
        }
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
    }

    @Override
    public void setProgress(int duration, int position) {
    }

    @Override
    public void onLockStateChanged(boolean isLock) {
    }

    public Text getTvMessage() {
        return mTvMessage;
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                break;
            case TouchEvent.POINT_MOVE:
            case TouchEvent.PRIMARY_POINT_UP:
                break;
            default:
                break;
        }
        return false;
    }
}
